/**
 * 用户中心
 *
 */
 import Taro from "@tarojs/taro";

import key from "./storageKeyMap"

/**
 * token getter && setter
 */
let token = "";

function getToken() {
  return token;
}

function setToken(value) {
  token = value;
}

function getLocalToken() {
  return Taro.getStorageSync(key.LOGIN_TOKEN_KEY);
}

function setLocalToken(value) {
  
  setToken(value);
  Taro.setStorageSync(key.LOGIN_TOKEN_KEY, value);
}

function clearLocalToken() {
  setToken("");
  Taro.removeStorageSync(key.LOGIN_TOKEN_KEY);
}

let userInfo = Taro.getStorageSync(key.USERINFO_KEY) || {};

function setUserInfo(value) {
  userInfo = value;
}

function getUserInfo() {
  return userInfo;
}

function getLocalUserInfo() {
  return Taro.getStorageSync(key.USERINFO_KEY);
}

function setLocalUserInfo(value, type) {
  if (type) {
    value.dataExpiration = Date.parse(new Date()) + 30 * 24 * 60 * 60 * 1000; // 缓存一个月
  }
  setUserInfo(value);
  Taro.setStorageSync(key.USERINFO_KEY, value);
}


function setLocalCompanyInfo(info) {
  Taro.setStorageSync(key.COMPANYINFO, info);
}

function getLocalCompanyInfo() {
  return Taro.getStorageSync(key.COMPANYINFO);
}

export {
  getToken,
  setToken,
  getLocalToken,
  setLocalToken,
  clearLocalToken,
  setUserInfo,
  getUserInfo,
  setLocalUserInfo,
  getLocalUserInfo,
  setLocalCompanyInfo,
  getLocalCompanyInfo,
};
